//
//@File    :	12.5_string1.cpp
//@Time    :	2021/09/08 12:39:41
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	String class methods
//

#include <cstring>//string.h for some
#include "12.4_string1.h"//includes<iostream>
using std::cin;
using std::cout;

//intializing static class namber
int String ::num_strings=0;
//static method
int String::HowMany()
{
    return num_strings;
}

//class methods
String::String(const char * s)
{
    len=std::strlen(s);//set size
    str=new char[len+1];//allot storage
    std::strcpy(str,s);//initialize pointer
    num_strings++;
}
String::String()
{
    len=4;
    str=new char[1];
    str[0]='\0';//default string
    num_strings++;
}
String::String(const String & st)//复制构造函数
{
    num_strings++;//handle static member update
    len=st.len;//same length
    str=new char [len+1];//allot space
    std::strcpy(str,st.str);//copy string to new location
}
String::~String()
{
    --num_strings;//reuired
    delete [] str;//reuired
}

//overloaded operator methods
    //assign a String to a String
String & String::operator=(const String & st)
{
    if (this==&st)
    {
        return *this;   
    }
    delete [] str;
    len=st.len;
    str=new char[len+1];
    std::strcpy(str,st.str);
    return *this;
}

    //assign a C string to a string
String & String::operator=(const char *s)
{
    delete [] str;
    len=std::strlen(s);
    str=new char[len+1];
    std::strcpy(str,s);
    return *this;
}
    //read-write char access for non-const String
char & String::operator[](int i)
{
    return str[i];
}
    //read-only char access for const String
const char & String::operator[](int i) const
{
    return str[i];
}

//overloaded operator friends
bool operator<(const String &st1,const String &st2)
{
    return (std::strcmp(st1.str,st2.str)<0);
}
bool operator>(const String & st1,const String &st2)
{
    return (st2<st1);
}
bool operator==(const  String &st1,const String &st2)
{
    return (std::strcmp(st1.str,st2.str)==0);
}
    //simple String output
ostream & operator<<(ostream & os,const String &st)
{
    os<<st.str;
    return os;
}
    //quick and dirty String input
istream & operator>>(istream & is,String & st)
{
    char temp[String::CINLIM];
    is.get(temp,String::CINLIM);
    if (is)
    {
        st=temp;
    }
    while (is&&is.get()!='\n')//文件尾或空行时中止
    {
        continue;
    }
    return is;
}